package com.jiaquyun.jcps.mode.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.jiaquyun.jcps.mode.BaseViewModel
import com.jiaquyun.jcps.entity.*
import com.jiaquyun.jcps.mode.manager.OrderManager
import com.jiaquyun.jcps.utils.AMapLocationUtils
import com.jiaquyun.jcps.utils.HttpWebSocketUtils
import com.lemlin.network.HttpServiceData
import com.lemlin.network.HttpServiceResponse
import com.lemlin.network.ViewModelSubscribe
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit


class TodayViewModel(application: Application) : BaseViewModel(application) {

    private val orderManager: OrderManager = OrderManager()

    val orderTodayDetailsLiveData =
        MutableLiveData<HttpServiceResponse<OrderTodayDetailsResponse>>()

    val orderTodayListLiveData =
        MutableLiveData<HttpServiceResponse<OrderTodayListResponse>>()

    /**
     *完成订单详情
     * */
    fun orderDetailsToday(riderOrderId: String) {
        val listener = ViewModelSubscribe<OrderTodayDetailsResponse>(orderTodayDetailsLiveData)
        val disposable: Disposable = orderManager.orderDetailsToday(riderOrderId, listener)
        dispose.add(disposable)
    }

    private var page: Int = 1
    private var limit: Int = 30

    /**
     *接单记录
     * */
    fun orderLog(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<OrderTodayListResponse>(orderTodayListLiveData) {
            override fun onSuccess(resp: OrderTodayListResponse) {
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            orderManager.orderLog(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


}